package com.wtwd.campus.method;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.VerifyResult;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.SetBodyToRequestUtils;
import com.wtwd.campus.utils.Sha256Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author ldaoliang
 * @Date create in 10:30 2021/7/5
 * @Description
 */
public class PostPutEncrypt implements EncryptVerify {

    private static final Logger logger = LoggerFactory.getLogger(PostPutEncrypt.class);

    @Override
    public VerifyResult verify(String sign, String token, HttpServletRequest request, RedisService redisService) {
        VerifyResult verifyResult = new VerifyResult();
        //使用流读取body中的json
        SetBodyToRequestUtils requestWrapper = new SetBodyToRequestUtils(request);
        JSONObject newResult = requestWrapper.getBody();
        if (Sha256Utils.SHA256(token + newResult.toJSONString()).equalsIgnoreCase(sign)) {
            requestWrapper.addHeader("Access-Token", token);
            verifyResult.setResult(true);
            verifyResult.setHttpServletRequest(requestWrapper);
        } else {
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.REQUEST_ILLEGAL);
        }
        return verifyResult;
    }
}
